package com.ruanruan.structure.graph;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 使用邻接矩阵实现图
 */
public class AdjMatrix implements Graph{

    //顶点个数
    private int V;
    //边的个数
    private int E;
    //存储图的邻接矩阵
    private int[][] adj;

    public AdjMatrix(String filename) {
        File file = new File(filename);
        try (Scanner scanner = new Scanner(file)) {
            //获取图的节点
            V = scanner.nextInt();
            if (V < 0) {
                throw new IllegalArgumentException("V must be non-negative");
            }
            adj = new int[V][V];
            //获取图的边
            E = scanner.nextInt();
            if (E < 0) {
                throw new IllegalArgumentException("E must be non-negative");
            }

            for (int i = 0; i < E; i++) {
                //验证ab的合法性
                int a = scanner.nextInt();
                validateVertex(a);
                int b = scanner.nextInt();
                validateVertex(b);
                //处理自环边与平行边
                if (a == b) {
                    throw new IllegalArgumentException("self loop is detected");
                }
                if (adj[a][b] == 1) {
                    throw new IllegalArgumentException("parallel edges are detected");
                }
                adj[a][b] = 1;
                adj[b][a] = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证顶点是否合法
     *
     * @param v 顶点
     */
    public void validateVertex(int v) {
        if (v < 0 || v >= V) {
            throw new IllegalArgumentException("vertex " + v + "is invalid");
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("V: %s, E: %s\n", V, E));
        for (int[] row : adj) {
            for (int col : row) {
                sb.append(col);
                sb.append(" ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }


    /**
     * 获取图的顶点个数
     *
     * @return 图的顶点个数
     */
    @Override
    public int V() {
        return V;
    }

    /**
     * 获取图的边的个数
     *
     * @return 图的边的个数
     */
    @Override
    public int E() {
        return E;
    }

    /**
     * 判断两点顶点是否相连
     *
     * @param v 顶点1
     * @param w 顶点2
     * @return true相连, false不相连
     */
    @Override
    public boolean hasEdge(int v, int w) {
        validateVertex(v);
        validateVertex(w);
        return adj[v][w] == 1;
    }

    /**
     * 获取与v顶点相连的所有节点
     *
     * @param v 顶点
     * @return 与顶点v连接的顶点集合
     */
    @Override
    public Iterable<Integer> adj(int v) {
        validateVertex(v);
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < V; i++) {
            if (adj[v][i] == 1) {
                res.add(i);
            }
        }
        return res;
    }

    /**
     * 获取顶点的度（顶点有多少个临边）
     *
     * @param v 顶点
     * @return 顶点的度
     */
    @Override
    public int degree(int v) {
        validateVertex(v);
        int degree = 0;
        for (int i = 0; i < V; i++) {
            if (adj[v][i] == 1) {
                degree++;
            }
        }
        return degree;
    }
}
